﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_46 : BaseProblem
    {
        public override object GetResult()
        {
            const int step = 100000;
            long curstep = step;

            var val = new HashSet<long>(MathLogic.GetPrimeList(curstep, true));
            var cur = 33;
            while (true)
            {
                cur = cur + 2;
                if (cur > curstep)
                {
                    curstep += step;
                    val = new HashSet<long>(MathLogic.GetPrimeList(curstep, true));
                }
                if (val.Contains(cur)) continue;
                foreach (var l in val)
                {
                    if (l > cur) 
                    {
                        return cur;
                    }
                    var vl = cur - l;
                    if (vl %2!=0) continue;
                    double tmp = Math.Sqrt(vl/2);
                    if (tmp == Math.Truncate(tmp)) break;
                }

            }
        }

        public override string Problem
        {
            get
            {
                return @"It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

9 = 7 + 212
15 = 7 + 222
21 = 3 + 232
25 = 7 + 232
27 = 19 + 222
33 = 31 + 212
It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 5777;
            }
        }
    }
}
